package com.example.front.mapper;

import com.example.common.dto.Pagination;
import com.example.front.dto.GatewaySearchDto;
import com.example.front.dto.WorkshopSearchDto;
import com.example.front.entity.Gateway;
import com.example.front.entity.Workshop;
import com.example.passport.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface WorkshopMapper {
    @Insert("INSERT INTO `workshop`" +
            " (`tenant_id`,`user_id`,`name`,`factory_id`) VALUES" +
            " (#{tenantId}, #{userId}, #{name},#{factoryId})")
    void create(Workshop workshop);

    @Update("UPDATE `workshop` SET " +
            "`factory_id`=#{workshop.factoryId}," +
            "`name` = #{workshop.name} " +
            ", `user_id` = #{workshop.userId}" +
            " WHERE `id`=#{workshop.id} AND `tenant_id`=#{workshop.tenantId} AND `status`!=" + User.STATUS_DELETE)
    int update(@Param("workshop") Workshop workshop);

    @Select("<script>" +
            "SELECT * FROM `workshop`" +
            "<where>" +
            "<if test='condition.id!=0'> AND `id`=#{condition.id}</if>" +
            "<if test='condition.factoryId!=null'> AND `factory_id`=#{condition.factoryId}</if>" +
            "<if test='condition.name!=null'> AND `name` LIKE CONCAT('%',#{condition.name},'%') </if>" +
            "<if test='condition.userId!=null'> AND `user_id`=#{condition.userId} </if>" +
            " AND `tenant_id`=#{condition.tenantId}  AND `status`!=" + User.STATUS_DELETE+
            "</where>" +
            " ORDER BY `created_at` DESC LIMIT #{pagination.offset}, #{pagination.limit}" +
            "</script>")
    List<Workshop> findAll(@Param("condition") WorkshopSearchDto condition, @Param("pagination") Pagination pagination);

    @Select("<script>" +
            "SELECT COUNT(*) FROM `workshop`" +
            "<where>" +
            "<if test='condition.id!=0'> AND `id`=#{condition.id}</if>" +
            "<if test='condition.name!=null'> AND `name` LIKE CONCAT('%',#{condition.name},'%') </if>" +
            "<if test='condition.userId!=null'> AND `user_id`=#{condition.userId}</if>" +
            " AND `tenant_id`=#{condition.tenantId}  AND `status`!=" + User.STATUS_DELETE +
            "</where>" +
            "</script>")
    long count(@Param("condition") WorkshopSearchDto condition);

    @Update("UPDATE `workshop` SET `status` ="+ User.STATUS_DELETE+
            " WHERE `id`=#{id} AND `tenant_id`=#{tenantId} AND `status`!=" + User.STATUS_DELETE)
    int deleteById(long id, long tenantId);

    @Select("SELECT * FROM workshop WHERE `tenant_id` = #{tenantId} AND `name` = #{name} AND `status`!=" + User.STATUS_DELETE+" LIMIT 1")
    Workshop findByName(@Param("tenantId") long tenantId, @Param("name") String name);

    @Select("SELECT * FROM workshop WHERE `tenant_id` = #{tenantId} AND `user_id` = #{userId} AND `status`!=" + User.STATUS_DELETE+" LIMIT 1")
    Workshop findByIp(@Param("tenantId") long tenantId, @Param("ip") String userId);

    @Select("SELECT * FROM workshop WHERE `tenant_id` = #{tenantId} AND `id` = #{id}  AND `name` = #{name} AND `status`!=" + User.STATUS_DELETE+" LIMIT 1")
    Workshop findByIdName(@Param("tenantId") long tenantId, @Param("id") long id , @Param("name") String name);

    @Select("SELECT * FROM workshop WHERE `tenant_id` = #{tenantId} AND `id` = #{id}  AND `user_id` = #{userId} AND `status`!=" + User.STATUS_DELETE+" LIMIT 1")
    Workshop findByIdIp(@Param("tenantId") long tenantId, @Param("id") long id ,  @Param("ip") String ip);

    @Select("SELECT * FROM workshop WHERE `tenant_id` = #{tenantId} AND `id` = #{id} AND `status`!=" + User.STATUS_DELETE)
    Workshop findById(@Param("id") long id,@Param("tenantId") long tenantId);

    @Select("SELECT * FROM workshop WHERE  `id` = #{id} AND `status`!=" + User.STATUS_DELETE)
    Workshop findByID(@Param("id") long id);

}
